package com.yanqu.road.server.manager.activity.xingyou;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.thread.ThreadTaskManger;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.xingyou.enums.eXingYouGridTypes;
import com.yanqu.road.server.manager.activity.xingyou.inter.*;
import com.yanqu.road.server.manager.activity.xingyou.inter.grid.*;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXingYouShanShuiMgr extends TempCrossMgr {

    //activityId,groupId
    public final static Map<Integer, Map<Integer, CrossXingYouActivity>> activityMap = new ConcurrentHashMap<>();

    private static ThreadTaskManger threadPool;

    public static synchronized CrossXingYouActivity getActivity(int activityId, int groupId) {
        // 【活动ID判断】
        if (activityMap.get(activityId) == null) {
            activityMap.put(activityId, new ConcurrentHashMap<>());
        }
        Map<Integer, CrossXingYouActivity> groupMap = activityMap.get(activityId);

        // 【分组ID判断】
        UnionActivityGroup groupData = Cross2UnionActivityGroupMgr.getGroupData(activityId, groupId);
        if (groupData == null) {
            return null;
        }
        if (!groupMap.containsKey(groupId)) {
            // 读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            if (activityInfo == null) {
                return null;
            }
            // 不在展示期也不处理了
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                return null;
            }
            CrossXingYouActivity activity = new CrossXingYouActivity(activityInfo, groupId);
            groupMap.put(groupId, activity);
        }
        return groupMap.get(groupId);
    }

    public static Map<Integer, Map<Integer, CrossXingYouActivity>> getActivityMap() {
        return activityMap;
    }

    @Override
    public boolean save() {
        if (activityMap.size() > 0) {
            getLogger().info("shanshui save start");
        }
        for (Map.Entry<Integer, Map<Integer, CrossXingYouActivity>> entry : activityMap.entrySet()) {
            for (Map.Entry<Integer, CrossXingYouActivity> activityEntry : entry.getValue().entrySet()) {
                activityEntry.getValue().save();
            }
        }
        if (activityMap.size() > 0) {
            getLogger().info("shanshui save end");
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        getLogger().info("shanshui reload config begin...");
        long now = System.currentTimeMillis() / 1000;
        for (Map.Entry<Integer, Map<Integer, CrossXingYouActivity>> entry : activityMap.entrySet()) {
            for (Map.Entry<Integer, CrossXingYouActivity> activityEntry : entry.getValue().entrySet()) {
                CrossXingYouActivity activity = activityEntry.getValue();
                if (now < activity.getConfig().getActivityInfo().getEndShowTime()) {
                    getLogger().info("shanshui reload config activity {}", activity.getConfig().getActivityInfo().getActivityId());
                    activity.reloadConfig();
                }
            }
        }
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        // 获取活动
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        reloadActivity(nowTimeStr);

        //初始化
        if(null == threadPool) {
            threadPool = new ThreadTaskManger(1, "send-captor-reward", 1);
        }
        return true;
    }

    public static IXingYouGridHandler getIXingYouGridHandler(int grid){
        if(grid == eXingYouGridTypes.One.getId()){
            return new DaNeiGridHandler();
        }
        if(grid == eXingYouGridTypes.Two.getId()){
            return new EmptyGridHandler();
        }
        //3/18/19
        if(grid == eXingYouGridTypes.Three.getId() || grid == eXingYouGridTypes.Eighteen.getId() || grid == eXingYouGridTypes.Nineteen.getId() ){
            return new SliverGridHandler();
        }
        if(grid == eXingYouGridTypes.Four.getId() || grid == eXingYouGridTypes.Five.getId() || grid == eXingYouGridTypes.EXPLORE.getId()){
            return new ExplortGridHandler();
        }
        if(grid == eXingYouGridTypes.Six.getId()){
            return new TouchEventHandler();
        }
        //7/8/9/10
        if(grid == eXingYouGridTypes.Seven.getId() || grid == eXingYouGridTypes.Eight.getId() || grid == eXingYouGridTypes.Nine.getId() || grid == eXingYouGridTypes.Ten.getId()){
            return new BuildGridHandler();
        }
        if(grid == eXingYouGridTypes.Eleven.getId()){
            return new BaoHuaGridHandler();
        }
        if(grid == eXingYouGridTypes.Twelve.getId()){
            return new ShipGridHandler();
        }
        if(grid == eXingYouGridTypes.Thirteen.getId()){
            return new WishGridHandler();
        }
        if(grid == eXingYouGridTypes.Fourteen.getId()){
            return new SecondGridHandler();
        }
        if(grid == eXingYouGridTypes.Fifteen.getId()){
            return new MaZuGridHandler();
        }
        if(grid == eXingYouGridTypes.Sixteen.getId()){
            return new YuMenGuanGridHandler();
        }
        if(grid == eXingYouGridTypes.Seventeen.getId()){
            return new TzGridHandler();
        }
        return null;
    }

    public static ThreadTaskManger getThreadPool() {
        return threadPool;
    }

    /**
     * 提前加载活动
     */
    public static void advanceLoadActivityTask() {
        // 提前一小时加载活动
        long time = System.currentTimeMillis() + DateHelper.HOUR_MILLIONS;
        String timeString = DateHelper.getCurrentDateTimeString(time);
        reloadActivity(timeString);
    }

    private static void reloadActivity(String nowTimeStr) {
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.XingYouShanShui.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
            if (null != activityGroupMap) {
                for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                    if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()) {
                        getActivity(activityGroup.getActivityId(), activityGroup.getGroupId());
                    }
                }
            }
        }
    }

    @Override
    public boolean removeExpireData(){
        List<Integer> removeList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for(int activityId : activityMap.keySet()){
            Map<Integer, CrossXingYouActivity> map = activityMap.get(activityId);
            for (Map.Entry<Integer, CrossXingYouActivity> entry : map.entrySet()) {
                CrossXingYouActivity activity = entry.getValue();
                if (activity.getConfig().getActivityInfo() == null || (nowTime > activity.getConfig().getActivityInfo().getEndShowTime() * 1000 + DateHelper.DAY_MILLIONS)) {
                    removeList.add(activityId);
                    break;
                }
            }
        }
        for(int activityId : removeList){
            activityMap.remove(activityId);
            getLogger().debug("remove activity {}", activityId);
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
